package com.cql.service.business.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cql.common.core.DO.UserDO;
import com.cql.common.core.DO.UserRelationDO;
import com.cql.common.utils.StringUtils;
import com.cql.mapper.UserMapper;
import com.cql.mapper.UserRelationMapper;
import com.cql.service.business.UserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author cql
 */
@Service("userRelationService")
public class UserRelationServiceImpl extends ServiceImpl<UserRelationMapper, UserRelationDO> implements UserRelationService {


    @Autowired
    private UserRelationMapper userRelationMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserRelationDO getUserRelation(String fromUser, String toUser) {
        if (StringUtils.isNull(fromUser) || StringUtils.isNull(toUser)) {
            return null;
        }
        if (fromUser.equals(toUser)) {
            return new UserRelationDO();
        }

        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.eq("account", fromUser);
        wrapper.eq("status", 0);
        UserDO fromUserDO = userMapper.selectOne(wrapper);

        wrapper = new QueryWrapper<>();
        wrapper.eq("account", toUser);
        wrapper.eq("status", 0);
        UserDO toUserDO = userMapper.selectOne(wrapper);

        if (StringUtils.isNull(fromUserDO) || StringUtils.isNull(toUserDO)) {
            return null;
        }

        QueryWrapper<UserRelationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("from_user", fromUserDO.getUserId());
        queryWrapper.eq("to_user", toUserDO.getUserId());

        UserRelationDO relationDO = userRelationMapper.selectOne(queryWrapper);


        return relationDO;

    }
}